package cn.kgc.controller;

import cn.kgc.commons.exception.HttpException;
import cn.kgc.entity.PmsCategory;
import cn.kgc.service.PmsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.PmsAttrGroup;
import cn.kgc.dto.PmsAttrGroupAddDTO;
import cn.kgc.dto.PmsAttrGroupUpdateDTO;
import cn.kgc.service.PmsAttrGroupService;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.PmsAttrGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 属性分组(PmsAttrGroup)表控制层
 *
 * @author makejava
 * @since 2022-11-29 15:03:28
 */
@RestController
@RequestMapping("pms/attr/group")
@Api(tags = "商品分组表")
@Validated
@Slf4j
public class PmsAttrGroupController {
    /**
     * 服务对象
     */
    @Resource
    private PmsAttrGroupService pmsAttrGroupService;
    @Resource
    private PmsCategoryService pmsCategoryService;


    /**
     * 分页查询属性组的信息
     *
     * @param current 当前页
     * @param pageSize 每页显示的条数
     * @param attrGroupName 属性组的名称
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页个数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "attrGroupName", value = "属性组名", paramType = "query", dataType = "string")
    })
    @CrossOrigin
    //@Cacheable(value = "pmsAttr",key = "#root.method.name")
    public IPage<PmsAttrGroup> selectAll(@RequestParam(defaultValue = "1") int current,
                                         @RequestParam(defaultValue = "5") int pageSize,
                                         @RequestParam(defaultValue = "") String attrGroupName) {
        log.info("分页查询属性组的信息，当前页：{}，每页个数：{}，属性组名：{}", current, pageSize, attrGroupName);
        //构建查询条件
        LambdaQueryWrapper<PmsAttrGroup> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(attrGroupName)){
            lambdaQueryWrapper.like(PmsAttrGroup::getAttrGroupName,attrGroupName);
        }
        //排序规则
        lambdaQueryWrapper.orderByAsc(PmsAttrGroup::getSort);
        IPage<PmsAttrGroup> page = new Page(current, pageSize);
        return pmsAttrGroupService.page(page,lambdaQueryWrapper);
    }

    @GetMapping("/list2")
    @CrossOrigin
    @ApiOperation(value = "通过商品分类id查询商品对应的属性组的分组信息", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "categoryId", value = "分类id", paramType = "query", dataType = "int")
    @Cacheable(value = "pmsAttrGroup", key = "#root.method.name+#categoryId")
    public List<PmsAttrGroup> selectByCategoryId(@NotNull(message = "商品分类不能为空") @RequestParam Long categoryId) {
        LambdaQueryWrapper<PmsAttrGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsAttrGroup::getCatelogId, categoryId);
        return pmsAttrGroupService.list(lambdaQueryWrapper);
    }

    /**
     * 新增数据
     *
     * @param pmsAttrGroupDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    @CacheEvict(value = "pmsAttrGroup",key = "'selectByCategoryId'+#pmsAttrGroupDTO.catelogId")
    public boolean insert(@RequestBody PmsAttrGroupAddDTO pmsAttrGroupDTO) {
  /*      long catelogId=pmsAttrGroupDTO.getCatelogId();
        PmsCategory pmsCategory = pmsCategoryService.getById(catelogId);
        if (pmsCategory==null){
            throw new HttpException(20002,500);
        }
        if (pmsCategory.getCatLevel()!=3){
            throw new HttpException(20003,500);
        }*/
        PmsAttrGroup pmsAttrGroup = new PmsAttrGroup();
        BeanUtils.copyProperties(pmsAttrGroupDTO, pmsAttrGroup);
        return this.pmsAttrGroupService.save(pmsAttrGroup);
    }

    /**
     * 修改数据
     *
     * @param pmsAttrGroupDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    @CacheEvict(value = "pmsAttrGroup",key = "'selectByCategoryId'+#pmsAttrGroupDTO.catelogId")
    public boolean update(@RequestBody PmsAttrGroupUpdateDTO pmsAttrGroupDTO) {
        PmsAttrGroup pmsAttrGroup = this.pmsAttrGroupService.getById(pmsAttrGroupDTO.getId());
        BeanUtils.copyProperties(pmsAttrGroupDTO, pmsAttrGroup);
        return this.pmsAttrGroupService.updateById(pmsAttrGroup);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "catelogId", value = "分类的id", paramType = "query", dataType = "int")
    })
    @CrossOrigin
    @CacheEvict(value = "pmsAttrGroup",key = "'selectByCategoryId'+#catelogId")
    public boolean delete(@RequestParam Long id,@RequestParam Long catelogId) {
        //删除属性组的时候，需要删除属性组下的所有属性
        return pmsAttrGroupService.deleteAttrGroupAndRelation(id);
    }
}
